package crp.services.api.graphql.models;

import com.qwlabs.lang.F2;
import crp.core.workflow.domain.models.Approval;
import crp.core.workflow.domain.models.ApprovalStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.microprofile.graphql.Ignore;
import org.eclipse.microprofile.graphql.Type;

import java.time.Instant;

@Type("Approval")
@Getter
@Setter
@Builder
@AllArgsConstructor
public class ApprovalPayload {
    @Ignore
    private final String submittedBy;
    private final Instant submittedAt;
    @Ignore
    private final String approvedBy;
    private final Instant approvedAt;
    private final String externalId;
    private final ApprovalStatus status;

    public static ApprovalPayload of(Approval approval) {
        return F2.ifPresent(approval, () ->
                ApprovalPayload.builder()
                        .submittedBy(approval.getSubmittedBy())
                        .submittedAt(approval.getSubmittedAt())
                        .approvedBy(approval.getApprovedBy())
                        .approvedAt(approval.getApprovedAt())
                        .externalId(approval.getExternalId())
                        .status(approval.getStatus())
                        .build());

    }
}
